<?php
// +----------------------------------------------------------------------
// | snake
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2022 http://baiyf.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: NickBai <1902822973@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\model;

use think\Model;
use think\Db;

class BusinessModel extends Model
{
    // 确定链接表名
    protected $table = 'wb_buser';

    /**
     * 查询商业用户
     * @param $where
     * @param $offset
     * @param $limit
     */
    public function getBusinesssByWhere($where, $offset, $limit)
    {
//        halt(111);


        return $res= $this->table('wb_buserinfo')->alias('i')
            ->join('wb_buser b','b.id=i.uid')
            ->field('*')
            ->where($where)
            ->where('b.role','=','2')
            ->limit($offset, $limit)->order('b.id desc')->select();


    }

    /**
     * 根据搜索条件获取所有的商业用户数量
     * @param $where
     */
    public function getAllBusinesss($where)
    {

        return $this->table('wb_buser')->alias('b')
            ->join('wb_buserinfo i','b.id=i.uid')
            ->field('*')
            ->where($where)
            ->where('b.role','=','2')
            ->count();

    }

    // 详情页
    public function show($id)
    {
//        return $this->table('wb_buser')->alias('b')
//            ->join('wb_buserinfo i','b.id=i.uid')
//            ->field('*')
//            ->order('b.id desc')->find($id);
    }


    /**
     * 根据管理员id获取角色信息
     * @param $id
     */
    public function getOneBusiness($id)
    {
        return $this->where('id', $id)->find();
    }

    /**
     * 改变用户状态
     * @param $id $s
     */
    public function editStatus($id ,$s)
    {
        $t = $s==1?2:1;
        $data = ['status'=>$t];
        try{

            $this->where('id', $id )->update($data);
            return msg(1, '', '修改成功');

        }catch(\Exception $e){
            return msg(-1, '', $e->getMessage());
        }
    }
}